home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / IFFParse / apps / ILBMLoad / ILBMLoad.c next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  5.9 KB  |  232 lines

  1. /* ILBMLoad.c 05/91  C. Scheppner CBM
  2.  *
  3.  * Example which
  4.  *  - first queries an ILBM to determine size and mode
  5.  *  - then opens an appropriate screen and window
  6.  *  - then loads the ILBM into the already opened screen
  7.  *
  8.  * For clipboard, use filename -c[unit] (like -c, -c1, -c2, etc.)
  9.  *
  10.  * requires linkage with several IFF modules
  11.  * see Makefile
  12.  */
  13.  
  14. #include "iffp/ilbmapp.h"
  15.  
  16.  
  17. #ifdef LATTICE
  18. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  19. int chkabort(void) { return(0); }  /* really */
  20. #endif
  21.  
  22. void cleanup(void);
  23. void bye(UBYTE *s,int error);
  24.  
  25. #define MINARGS 2
  26. char *vers = "\0$VER: ILBMLoad 37.5";
  27. char *Copyright = "ILBMLoad v37.5 (Freely Redistributable)";
  28. char *usage = "Usage: ILBMLoad ilbmname (-c[unit] for clipboard";
  29.  
  30.  
  31. struct Library *IntuitionBase  = NULL;
  32. struct Library *GfxBase        = NULL;
  33. struct Library *IFFParseBase   = NULL;
  34.  
  35. /* Note - these fields are also available in the ILBMInfo structure */
  36. struct   Screen         *scr;         /* for ptr to screen structure */
  37. struct   Window         *win;         /* for ptr to window structure */
  38. struct   RastPort       *wrp;         /* for ptr to RastPort  */
  39. struct   ViewPort       *vp;          /* for ptr to Viewport  */
  40.  
  41. struct   IntuiMessage   *msg;
  42.  
  43. struct   NewWindow      mynw = {
  44.    0, 0,                                  /* LeftEdge and TopEdge */
  45.    0, 0,                                /* Width and Height */
  46.    -1, -1,                                /* DetailPen and BlockPen */
  47.    VANILLAKEY|MOUSEBUTTONS,               /* IDCMP Flags with Flags below */
  48.    BACKDROP|BORDERLESS|SMART_REFRESH|NOCAREREFRESH|ACTIVATE|RMBTRAP,
  49.    NULL, NULL,                            /* Gadget and Image pointers */
  50.    NULL,                                  /* Title string */
  51.    NULL,                                  /* Screen ptr null till opened */
  52.    NULL,                                  /* BitMap pointer */
  53.    50, 20,                                /* MinWidth and MinHeight */
  54.    0 , 0,                                 /* MaxWidth and MaxHeight */
  55.    CUSTOMSCREEN                           /* Type of window */
  56.    };
  57.  
  58.  
  59. BOOL   FromWb;
  60.  
  61.  
  62. /* ILBM Property chunks to be grabbed
  63.  * List BMHD, CMAP and CAMG first so we can skip them when we write
  64.  * the file back out (they will be written out with separate code)
  65.  */
  66. LONG    ilbmprops[] = {
  67.         ID_ILBM, ID_BMHD,
  68.         ID_ILBM, ID_CMAP,
  69.         ID_ILBM, ID_CAMG,
  70.         ID_ILBM, ID_CCRT,
  71.         ID_ILBM, ID_AUTH,
  72.         ID_ILBM, ID_Copyright,
  73.         TAG_DONE
  74.         };
  75.  
  76. /* ILBM Collection chunks (more than one in file) to be gathered */
  77. LONG    ilbmcollects[] = {
  78.         ID_ILBM, ID_CRNG,
  79.         TAG_DONE
  80.         };
  81.  
  82. /* ILBM Chunk to stop on */
  83. LONG    ilbmstops[] = {
  84.         ID_ILBM, ID_BODY,
  85.         TAG_DONE
  86.         };
  87.  
  88.  
  89. UBYTE nomem[]  = "Not enough memory\n";
  90. UBYTE noiffh[] = "Can't alloc iff\n";
  91.  
  92.  
  93.  
  94. /* For our allocated ILBM frame */
  95. struct ILBMInfo  *ilbm;
  96.  
  97.  
  98. /* 
  99.  * MAIN 
  100.  */
  101. void main(int argc, char **argv)
  102.    {
  103.    UBYTE *ilbmname=NULL;
  104.    LONG error = 0L;
  105.  
  106.    FromWb = argc ? FALSE : TRUE;
  107.  
  108.    if((argc<MINARGS)||(argv[argc-1][0]=='?'))
  109.     {
  110.     printf("%s\n%s\n",Copyright,usage);
  111.         bye("",RETURN_OK);
  112.     }
  113.  
  114.    ilbmname = argv[1];
  115.  
  116.    /* Open Libraries */
  117.  
  118.    if(!(IntuitionBase = OpenLibrary("intuition.library", 0)))
  119.       bye("Can't open intuition library.\n",RETURN_WARN);
  120.       
  121.    if(!(GfxBase = OpenLibrary("graphics.library",0)))
  122.       bye("Can't open graphics library.\n",RETURN_WARN);
  123.  
  124.    if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  125.       bye("Can't open iffparse library.\n",RETURN_WARN);
  126.  
  127.  
  128.  
  129. /* 
  130.  * Alloc one ILBMInfo struct
  131.  */
  132.     if(!(ilbm = (struct ILBMInfo *)
  133.     AllocMem(sizeof(struct ILBMInfo),MEMF_PUBLIC|MEMF_CLEAR))) 
  134.         bye(nomem,RETURN_FAIL);
  135.  
  136. /*
  137.  * Here we set up our ILBMInfo fields for our
  138.  * application.
  139.  * Above we have defined the propery and collection chunks
  140.  * we are interested in (some required like BMHD)
  141.  */
  142.  
  143.     ilbm->ParseInfo.propchks    = ilbmprops;
  144.     ilbm->ParseInfo.collectchks    = ilbmcollects;
  145.     ilbm->ParseInfo.stopchks    = ilbmstops;
  146.  
  147.     ilbm->windef    = &mynw;
  148.  
  149. /* 
  150.  * Alloc IFF handle for frame
  151.  */
  152.     if(!(ilbm->ParseInfo.iff = AllocIFF())) bye(noiffh,RETURN_FAIL);
  153.  
  154. /* Normally you would use showilbm() to open an appropriate acreen
  155.  * and display an ILBM in it.
  156.  *
  157.  * However, here we are demonstrating
  158.  *  - first querying an ILBM to get its BMHD and CAMG (real or computed)
  159.  *  - then opening our own display
  160.  *  - then loading the ILBM into it
  161.  */
  162.  
  163.     if(!(error = queryilbm(ilbm,ilbmname)))
  164.     {
  165.     D(bug("ilbmload: after query, this ILBM is %ld x %ld x %ld, modeid=$%lx\n",
  166.         ilbm->Bmhd.w, ilbm->Bmhd.h, ilbm->Bmhd.nPlanes, ilbm->camg));
  167.  
  168.     /* Note - you could use your own routines to open your
  169.      * display, but if so, you must initialize ilbm->scr,
  170.      * ilbm->win, ilbm->wrp, ilbm->srp, and ilbm->vp for your display.
  171.      * Here we will use opendisplay() which will initialize
  172.      * those fields.
  173.      */
  174.     if(!(opendisplay(ilbm,
  175.             MAX(ilbm->Bmhd.pageWidth, ilbm->Bmhd.w),
  176.             MAX(ilbm->Bmhd.pageHeight,ilbm->Bmhd.h),
  177.             MIN(ilbm->Bmhd.nPlanes,MAXAMDEPTH),
  178.             ilbm->camg)))
  179.         {
  180.         printf("Failed to open display\n");
  181.         }
  182.     else
  183.         {
  184.         D(bug("ilbmload: opendisplay successful\n"));
  185.  
  186.         scr = ilbm->scr;
  187.         win = ilbm->win;
  188.  
  189.         if(!(error = loadilbm(ilbm, ilbmname)))
  190.         {
  191.             D(bug("ilbmload: loadilbm successful\n"));
  192.  
  193.         /* Note - we don't need to examine or copy any
  194.          * chunks from the file, so we will close file now
  195.          */
  196.         closeifile(ilbm);
  197.         ScreenToFront(ilbm->scr);
  198.         Wait(1<<win->UserPort->mp_SigBit);
  199.         unloadilbm(ilbm);    /* deallocs colors, closeifile if needed */
  200.         }
  201.         closedisplay(ilbm);
  202.          }
  203.           }
  204.  
  205.     if(error)    printf("%s\n",IFFerr(error));
  206.  
  207.     cleanup();
  208.     exit(RETURN_OK);
  209.     }
  210.  
  211.  
  212. void bye(UBYTE *s,int error)
  213.    {
  214.    if((*s)&&(!FromWb)) printf("%s\n",s);
  215.    cleanup();
  216.    exit(error);
  217.    }
  218.  
  219.  
  220. void cleanup()
  221.    {
  222.    if(ilbm)
  223.     {
  224.     if(ilbm->ParseInfo.iff)     FreeIFF(ilbm->ParseInfo.iff);
  225.     FreeMem(ilbm,sizeof(struct ILBMInfo));
  226.     }
  227.  
  228.    if(GfxBase)              CloseLibrary(GfxBase);
  229.    if(IntuitionBase)     CloseLibrary(IntuitionBase);
  230.    if(IFFParseBase)      CloseLibrary(IFFParseBase);
  231.    }
  232.